/**
 * copyright@hyht
 */
package com.wondream.myframework.app.common.util.bbgJwt;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author cailongfei
 * @Date 2016-3-15
 * @FileName HyAfterAnalysisUtil.java
 * @TODO 时候分析工具类</br>
 */
public class HyDateUtil {
    public static final String NO_SPLIT_DATE_FULL_SS = "yyyyMMddHHmmss";
    public static final String NO_SPLIT_DATE_YMD = "yyyyMMdd";
    public static final String NO_SPLIT_MONTH_YMD = "yyyyMM";
    public static final String NO_SPLIT_MONTH_HMS = "HHmmss";
    public static final SimpleDateFormat SDF_DATE_FULL_SS = getDateFormat(NO_SPLIT_DATE_FULL_SS);
    public static final SimpleDateFormat SDF_NO_SPLIT_DATE_SHORT = getDateFormat(NO_SPLIT_DATE_YMD);
    public static final SimpleDateFormat SDF_NO_SPLIT_SHORT = getDateFormat(NO_SPLIT_MONTH_YMD);
    public static SimpleDateFormat getDateFormat(String format) {
        return new SimpleDateFormat(format);
    }
    /**
     * 风控计算时间周期，只计算一个周期的开始日期
     * 
     * @param date
     * @param unit
     * @return
     * @throws Exception
     */
    public static String calculateTimePeriodStart(String date, String unit) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));

        if (StringUtils.equals(unit, "D")) {

        } else if (StringUtils.equals(unit, "M")) {// 获取本月开始日期
            calendar.set(Calendar.DATE, 1);
        } else if (StringUtils.equals(unit, "Y")) {// 获取本年开始日期
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DATE, 1);
        } else if (StringUtils.equals(unit, "W")) {// 获取本周开始日期，从周一开始
            int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (day_of_week == 0)
                day_of_week = 7;
            calendar.add(Calendar.DATE, -day_of_week + 1);
        } else if (StringUtils.equals(unit, "S")) {// 获取本季度开始日期
            int currentMonth = calendar.get(Calendar.MONTH) + 1;
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } else {
            throw new Exception("unit 不在枚举值内[Y,M,W,D,S]");
        }
        return sdf.format(calendar.getTime());
    }

    /**
     * 风控计算时间周期，计算一个周期的结束日期
     * 
     * @param date
     * @param unit
     * @return
     * @throws Exception
     */
    public static String calculateTimePeriodEnd(String date, String unit) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));

        if (StringUtils.equals(unit, "D")) {

        } else if (StringUtils.equals(unit, "M")) {// 获取本月结束日期
            calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        } else if (StringUtils.equals(unit, "Y")) {// 获取本年结束日期
            calendar.set(Calendar.MONTH, 11);
            calendar.set(Calendar.DATE, 31);
        } else if (StringUtils.equals(unit, "W")) {// 获取本周结束日期，周日
            int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (day_of_week == 0)
                day_of_week = 7;
            calendar.add(Calendar.DATE, -day_of_week + 7);
        } else if (StringUtils.equals(unit, "S")) {// 获取本季度结束日期
            int currentMonth = calendar.get(Calendar.MONTH) + 1;
            if (currentMonth >= 1 && currentMonth <= 3) {
                calendar.set(Calendar.MONTH, 2);
                calendar.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                calendar.set(Calendar.MONTH, 5);
                calendar.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                calendar.set(Calendar.MONTH, 8);
                calendar.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                calendar.set(Calendar.MONTH, 11);
                calendar.set(Calendar.DATE, 31);
            }
        } else {
            throw new Exception("unit 不在枚举值内[Y,M,W,D,S]");
        }
        return sdf.format(calendar.getTime());
    }

    /**
     * 计算时间是否是周期最后一天
     * 
     * @param date
     * @param unit
     * @return
     * @throws Exception
     */
    public static boolean calculateDatePeriodEnd(String date, String unit) throws Exception {
        String dateStr = calculateTimePeriodEnd(date, unit);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar curCalendar = Calendar.getInstance();
        Calendar PerCalendar = Calendar.getInstance();
        curCalendar.setTime(sdf.parse(date));// 当前时间
        PerCalendar.setTime(sdf.parse(dateStr));// 周期末时间
        if (PerCalendar.getTime().compareTo(curCalendar.getTime()) == 0) {
            return false;
        }
        return true;
    }
    public static String timeOffset(String time, int timeUnit, int offSet) throws HyException {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(SDF_DATE_FULL_SS.parse(time));
        } catch (ParseException e) {
            throw new HyException(e);
        }
        calendar.add(timeUnit, offSet);
        return SDF_DATE_FULL_SS.format(calendar.getTime());
    }
    /**
     * 计算两个时间差
     * @param startDateStr
     * @param endDateStr
     * @return
     * @throws ParseException
     */
    public static String getTimeCountDown(String startDateStr, String endDateStr) throws HyException {
        Date startDate,endDate = null;
        try {
            startDate = SDF_DATE_FULL_SS.parse(startDateStr);
            endDate = SDF_DATE_FULL_SS.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new HyException(e);
        }
        long offset = endDate.getTime() - startDate.getTime();
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        // 计算差多少天
        long day = offset / nd;
        // 计算差多少小时
        long hour = offset % nd / nh;
        // 计算差多少分钟
        long min = offset % nd % nh / nm;

        long sec = offset % nd % nh % nm / ns;
        // 计算差多少秒//输出结果
        StringBuffer offsetStr = new StringBuffer();
        if (day > 0) {
            offsetStr = offsetStr.append(day).append("天");
        }
        if (hour > 0) {
            offsetStr = offsetStr.append(hour).append("小时");
        }
        long minTemp = min;
        if (sec > 0) {
            offsetStr = offsetStr.append(minTemp + 1).append("分钟");
        } else {
            if (min > 0) {
                offsetStr = offsetStr.append(min).append("分钟");
            }
        }
        return offsetStr.toString();
    }
    /**
     * 获取系统日期yyyyMMdd
     *
     * @return
     */
    public static String getCurrentDate() {
        return getDateFormat(NO_SPLIT_DATE_YMD).format(new Date());
    }
    /**
     * 获取系统日期yyyyMMddHHmmss
     *
     * @return
     */
    public static String getCurrentFullTime() {
        return getDateFormat(NO_SPLIT_DATE_FULL_SS).format(new Date());
    }
    /**
     * 获取系统日期HHmmss
     *
     * @return
     */
    public static String getCurrentTime() {
        return getDateFormat(NO_SPLIT_MONTH_HMS).format(new Date());
    }
    /**
     * 获取系统日期yyyyMMdd
     *
     * @return
     */
    public static String getCurrentMonth() {
        return getDateFormat(NO_SPLIT_MONTH_YMD).format(new Date());
    }
    /**
     * 获取两个日期差
     *
     * @param startDateStr
     * @param endDateStr
     * @return days
     * @throws ParseException
     */
    public static int getDatesBetweenTwoDate(String startDateStr, String endDateStr) throws HyException {
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = SDF_NO_SPLIT_DATE_SHORT.parse(startDateStr);
            endDate = SDF_NO_SPLIT_DATE_SHORT.parse(endDateStr);
        } catch (ParseException e) {
            throw new HyException(e);
        }
        long offset = endDate.getTime() - startDate.getTime();
        long nd = 1000 * 24 * 60 * 60;
        // 计算差多少天
        int day = Integer.parseInt(String.valueOf(offset / nd));
        return day;
    }

    /**
     * 获取两个月期差
     *
     * @param startMonthStr
     * @param endDMonthStr
     * @return days
     * @throws ParseException
     */
    public static int getBetweenTwoMonth(String startMonthStr, String endDMonthStr) throws HyException {
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = SDF_NO_SPLIT_SHORT.parse(startMonthStr);
            endDate = SDF_NO_SPLIT_SHORT.parse(endDMonthStr);
        } catch (ParseException e) {
            throw new HyException(e);
        }
        long offset = endDate.getTime() - startDate.getTime();
        long nd = 1000 * 24 * 60 * 60;
        // 计算差多少天
        int day = Integer.parseInt(String.valueOf(offset / nd));
        return day;
    }

    public static void main(String[] args) {
        try {
            // String umonth=HyDateUtils.calculateDate("20161201", "-", "M", 1);
            // calculateTimePeriodEnd("20161030","S");
            // SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyyMM");

            // System.err.println( "20160613".substring(0, 6));

            System.err.println(getDatesBetweenTwoDate("20190301", "20190201"));
            System.err.println(getBetweenTwoMonth("201902", "201902"));
            // System.err.println(HyAfterAnalysisUtil.calculateTimePeriodStart("20160403","W"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
